package com.ji.server;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Title:
 * Description:
 * Author:吉
 * Since:2017/5/19
 * Version:1.1.0
 */
public class NamedThreadFactory implements ThreadFactory {

    private final String mPrefix;

    private final boolean mDaemo;
    private final ThreadGroup mGroup;
    private final AtomicInteger mThreadNum = new AtomicInteger(1);

    public NamedThreadFactory(String prefix, boolean daemo) {
        mPrefix = prefix + "-thread-";
        mDaemo = daemo;
        mGroup = Thread.currentThread().getThreadGroup();
    }

    @Override
    public Thread newThread(Runnable r) {
        String name = mPrefix + mThreadNum.getAndIncrement();
        Thread ret = new Thread(mGroup, r, name);
        ret.setDaemon(mDaemo);
        return ret;
    }
}
